﻿using System;
using System.Collections.Generic;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Model
{
    public class Rectangle : Shape
    {
        private Point lastPoint;

        //These were created for rectangle and all shapes that inherit from it
        protected int Height { get; private set; }
        protected int Width { get; private set; }

        public Rectangle(Point first)
        {
            FirstPoint = first;
        }

        public override Point LastPoint
        {
            get { return lastPoint; }

            set
            {
                lastPoint = value;
                SetWidthHeight();
            }
        }

        private void SetWidthHeight()
        {
            Width = Math.Abs(FirstPoint.X - LastPoint.X);
            Height = Math.Abs(FirstPoint.Y - LastPoint.Y);
        }

        public override void Draw(Graphics g)
        {
            if (LastPoint.IsEmpty)
                return;
           var pen =new Pen(this.Color,this.BorderWidth);
            var drawrect = new System.Drawing.Rectangle(UpperLeftCorner(),new Size(Width, Height));
            g.DrawRectangle(pen,drawrect);
            if(Fill)
            {       
                g.FillRectangle(new SolidBrush(this.FillColor),new System.Drawing.Rectangle(FillUpperrLeftCorner(),FillSize()));
            }
        }

        public Point UpperLeftCorner()
        {
            return new Point(Math.Min(FirstPoint.X, LastPoint.X), Math.Min(FirstPoint.Y, LastPoint.Y));
        }

        protected Point FillUpperrLeftCorner()
        {
            return new Point(Math.Min(FirstPoint.X, LastPoint.X)+BorderWidth, Math.Min(FirstPoint.Y, LastPoint.Y)-BorderWidth);// todo test
        }

        protected Size FillSize()
        {
            return new Size(new Point(Width-BorderWidth,Height-BorderWidth));// todo test for zero and negative
        }

        public override string ToString()
        {
            return string.Format("Upper Left Corner is: {0} \nWidth: {1}\nHeight: {2}", UpperLeftCorner(),
                                 Width, Height);
        }
    }
}